package com.fingard.dsp.bank.directbank.qlb03;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/02/26.
 */
public class QLB03Base extends DirectBase implements ITcpClient {

    private static TransIDHelper idHelper = new TransIDHelper(1, 99);

    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            String tmpCharset = getCharset();
            String tmpRecNow = new String(p_receiver.recBytes, 0, p_receiver.readedLength, tmpCharset);
            if (tmpRecNow.contains("</ap>")) {
                p_receiver.contentLength = p_receiver.readedLength;
                p_receiver.contentStartIndex = 0;
            }
        }
    }

    protected void writeCommonHeader(XmlTextWriter xmlWriter, QLB03Header p_bankHeader) throws Exception{
        xmlWriter.writeStartElement("ap");
        xmlWriter.writeStartElement("head");
        xmlWriter.writeElementString("tr_code", p_bankHeader.tr_code);
        xmlWriter.writeElementString("cms_corp_no", "");//银企直连客户号
        xmlWriter.writeElementString("user_no", "");//用户号
        xmlWriter.writeElementString("org_code", "");//机构号
        xmlWriter.writeElementString("serial_no", "");//交易流水号
        xmlWriter.writeElementString("req_no", p_bankHeader.req_no);//请求号
        xmlWriter.writeElementString("tr_acdt", Format.Date8Format.format(new Date()));//交易日期
        xmlWriter.writeElementString("tr_time", Format.Time6Format.format(new Date()));//交易时间
        xmlWriter.writeElementString("channel", "5");//渠道标识
        xmlWriter.writeElementString("sign", "");//签名标识
        xmlWriter.writeElementString("file_flag", p_bankHeader.file_flag);//传送0-报文1-文件
        xmlWriter.writeElementString("reserved", "");//备用字段
    }

    protected String getCharset(){
        return getBankConfig().getCharset("gbk");
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};
        WriteBankUrlLogLn( "请求银行的地址"+bankFront.serverIP + ":" + bankFront.serverPort);
        WriteBankLogStep2(p_reqStr);
        //发送银行前置
        TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.serverPort), this);
        tmpClient.timeoutMil=300000;
        TcpReceiver tmpReceiver = tmpClient.sendMessage(p_reqStr,getCharset());
        retStr[0] = tmpReceiver.connState;
        retStr[1] = tmpReceiver.getFullString(getCharset());
        if(StringHelper.isNullOrEmpty(retStr[1])){
        	retStr[1] = tmpReceiver.getConnMsg();
        }
        WriteBankLogStep3(retStr[1]);
        return retStr;
    }

    /**
     * 报头+报体
     * @param xml
     * @return
     */
    protected String requestBody(String xml){
        xml=xml.replaceAll(" ","");
        String reqString="";
        if(StringHelper.hasAnyChar(xml)){
            int length=xml.getBytes().length+2;
            String head=String.format("%-10s",length+"")+"00";
            reqString=head+xml;
        }
        return reqString;
    }
}
